<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\common\model\Message;
use app\common\model\User;

class Index extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
        // $map1 = [
        //     ['user_id', '=', input('get.user_id')],
        //     ['to_id', '=', input('get.to_id')]
        // ];
        // $map2 = [
        //     ['user_id', '=', input('get.to_id')],
        //     ['to_id', '=', input('get.user_id')]
        // ];
        return $this->fetch();
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    /**
     * 显示资源列表
     * 
     * @return \think\Response
     */
    public function getMessage($user_id, $to_id)
    {
        $data = Message::field('id, user_id, user_name, to_id, to_name, content, type, is_read, create_time')->whereOr([[['user_id', '=', addslashes($user_id)], ['to_id', '=', addslashes($to_id)]], [['user_id', '=', addslashes($to_id)],['to_id', '=', addslashes($user_id)]]])->order('create_time')->all();
        return json($data);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if ($request->isPost()) {
            $post = $request->post();
            $data['user_id'] = $post['user_id'];
            $data['user_name'] = $this->getNickName($post['user_id']);
            $data['to_id'] = $post['to_id'];
            $data['to_name'] = $this->getNickName($post['to_id']);
            $data['content'] = $post['content'];
            $data['type'] = $post['type'];
            $data['is_read'] = $post['is_read'];
            $result = Message::create($data);
            return $result ? json(200) : json(400);
        }
    }

    /**
     * 显示指定的资源
     * 
     * @param  int  $id
     * @return \think\Response
     */
    public function getNickName($id)
    {
        $nickname = User::where('id', addslashes($id))->value('nickname');
        return $nickname;
    }

    /**
     * 上传
     * 
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function upload(Request $request)
    {
        if ($request->isPost()) {
            $file = $request->file('file');
            $info = $file->rule('uniqid')->move( './uploads/image', true, false);
            // $post = $reqeust->post();
            // $data['user_id'] = $post['user_id'];
            // $data['user_name'] = $this->getNickName($post['user_id']);
            // $data['to_id'] = $post['to_id'];
            // $data['to_name'] = $this->getNickName($post['to_id']);
            // $data['content'] = $info->getFilename();
            // $data['type'] = 'image';
            return $info->getFilename();
        }
    }
}
